<template>
  <div>
    <p>
      <button class="btn btn-white btn-default btn-round" @click="add()" v-show="hasResource('010101')">
        <i class="ace-icon fa fa-edit"></i>
        新增
      </button>
      &nbsp;
      <button class="btn btn-white btn-default btn-round" @click="list(1)">
        <i class="ace-icon fa fa-refresh"></i>
        刷新
      </button>
    </p>

    <pagination ref="pagination" :list="list"></pagination>

    <table id="simple-table" class="table  table-bordered table-hover">
      <thead>
        <tr>
          <th>id</th>
          <th>登陆名</th>
          <th>昵称</th>
          <th>密码</th>
          <th>操作</th>
        </tr>
      </thead>

      <tbody>
        <tr v-for="user in users"
          :key="user.id"
          :index="user.id">
          <td>{{user.id}}</td>
          <td>{{user.loginName}}</td>
          <td>{{user.name}}</td>
          <td>{{user.password}}</td>

          <td>
            <div class="btn-group">
              <button class="btn btn-xs btn-info" @click="editPassword(user)" v-show="hasResource('010103')">
                <i class="ace-icon fa fa-key bigger-120"></i>
              </button>
              <button class="btn btn-xs btn-info" @click="edit(user)" v-show="hasResource('010101')">
                <i class="ace-icon fa fa-pencil bigger-120"></i>
              </button>
              <button class="btn btn-xs btn-danger" @click="del(user.id)" v-show="hasResource('010102')">
                <i class="ace-icon fa fa-trash-o bigger-120"></i>
              </button>
            </div>
          </td>
        </tr>
      </tbody>
    </table>

    <!-- Modal -->
    <div id="form-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
              aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">用户表单</h4>
          </div>
          <div class="modal-body">
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">登陆名</label>
                <div class="col-sm-10">
                    <input class="form-control" v-model="user.loginName" :disabled="user.id ">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">昵称</label>
                <div class="col-sm-10">
                    <input class="form-control" v-model="user.name">
                </div>
              </div>
              <div class="form-group" v-show="!user.id">
                <label class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                    <input class="form-control" type="password" v-model="user.password">
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" @click="save()">保存</button>
          </div>
        </div>
      </div>
    </div>

    <!--  密码修改modal  -->
    <div id="edit-password-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
              aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">修改密码</h4>
          </div>
          <div class="modal-body">
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">密码</label>
                <div class="col-sm-10">
                  <input class="form-control" type="password" name="password" v-model="user.password">
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" @click="savePassword()">保存</button>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
  import Pagination from "../../components/pagination"

  export default {
    name: "system-user",
    components: {Pagination},
    data() {
      return {
        user: {
          id: null,
          loginName: null,
          name: null,
          password: null,
        },
        users: [],
      }
    },
    mounted() {
      let _this = this
      _this.list(1)
    },
    methods: {
      /**
       * 获取用户（分页）
       * @param page
       */
      list(page) {
        let _this = this

        Loading.show()

        _this.$axios.post(process.env.VUE_APP_SERVER + '/system/admin/user/list', {
          page: page,
          size: _this.$refs.pagination.size
        }).then((resp) => {
          if (resp.data.success) {
            _this.users = resp.data.content.list
            _this.$refs.pagination.render(page, resp.data.content.total)

            Loading.hide()
          } else {
            Loading.hide()
          }
        })
      },

      /**
       * 添加用户（显示模态框）
       */
      add() {
        let _this = this
        _this.user = {
          id: null,
          loginName: null,
          name: null,
          password: null,
        }
        //  开启模态框
        $("#form-modal").modal('show')
      },

      /**
       * 编辑用户（显示模态框）
       */
      edit(user) {
        let _this = this
        //  Object.assign(target, source)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target)
        Object.assign(_this.user, user)
        //  开启模态框
        $("#form-modal").modal('show')
      },

      /**
       * 保存用户
       */
      save() {
        let _this = this

        //  保存校验
        if (1 != 1
          || !Validator.require(_this.user.loginName, "登陆名")
          || !Validator.length(_this.user.loginName, "登陆名", 1, 50)
          || !Validator.length(_this.user.name, "昵称", 1, 50)
          || !Validator.require(_this.user.password, "密码")
        ) {
          return;
        }
        // 密码加密(MD5)
        _this.user.password = hex_md5(_this.user.password + KEY)

        _this.$axios.post(process.env.VUE_APP_SERVER + '/system/admin/user/save', _this.user).then((resp) => {
          if (resp.data.success) {
            //  关闭模态框
            $("#form-modal").modal('hide')
            //  刷新表格
            _this.list(1)
            Toast.success('保存成功！')
          } else {
            Toast.warning(resp.data.message)
          }
        })
      },

      /**
       * 删除用户（根据id）
       * @param id
       */
      del(id) {
        let _this = this
        Confirm.show('删除用户后不可恢复，是否确认删除？', () => {
          _this.$axios.delete(process.env.VUE_APP_SERVER + '/system/admin/user/delete/' + id).then((resp) => {
            if (resp.data.success) {
              //  刷新表格
              _this.list(1)
              Toast.success('删除成功！')
            }
          })
        })
      },

      /**
       * 重置密码(显示模态框)
       * @param user
       */
      editPassword(user) {
        let _this = this
        Object.assign(_this.user, user)
        _this.user.password = ''
        //  开启重置密码模态框
        $("#edit-password-modal").modal('show')
      },

      /**
       * 保存重置的密码
       */
      savePassword() {
        let _this = this
        // 密码加密(MD5)
        _this.user.password = hex_md5(_this.user.password + KEY)

        _this.$axios.post(process.env.VUE_APP_SERVER + '/system/admin/user/save-password', _this.user).then((resp) => {
          if (resp.data.success) {
            //  关闭重置密码模态框
            $("#edit-password-modal").modal('hide')
            //  刷新表格
            _this.list(1)
            Toast.success('保存成功！')
          } else {
            Toast.warning(resp.data.message)
          }
        })
      },

      /**
       * 是否有权限
       * @param resourceId
       * @returns {boolean}
       */
      hasResource(resourceId) {
        return Tool.hasResource(resourceId)
      }
    }
  }
</script>

<style scoped>

</style>